import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController, ViewController, PopoverController } from 'ionic-angular';
import { ApiProvider } from '../../../providers/api/api';
import { NotificationProvider } from '../../../providers/notification/notification';
import { PAGE_COUNT } from '../../../app/app.constants';
import { transition } from '@angular/core/src/animation/dsl';

/**
 * Generated class for the StoreSelPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-store-sel',
  templateUrl: 'store-sel.html',
})
export class StoreSelPage {
  private houseId:string;
  private sp_code:any;
  private funid = 'sp_store';
  private items: Array<string>;
  private searchValue: string;
  private options = {
    start: 0,
    limit: PAGE_COUNT,//页面的大小，下面的的是查询条件
    where_sql:'sp_store.store_num > 0 ',
    where_type:'',
    where_value:'',
  };
  private searchTimer: any;
  private selItem;

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    private api: ApiProvider, 
    private notification: NotificationProvider,
    public modalCtrl: ModalController,
    public popoverCtrl: PopoverController,
    public viewCtrl: ViewController ) {
      this.houseId=navParams.get('house_id'); //页面之间的传值
      this.sp_code=navParams.get('sp_code'); 
    }
    
//第一次页面加载时候的查询，初始化页面数据
  ionViewDidLoad() {
    this.notification.showLoading();
      
    if(this.sp_code){
      this.options.where_sql="sp_store.store_num > 0 and sp_code= ? and sp_store.house_id like ? ";
      this.options.where_type="string;string;";
      this.options.where_value=+this.sp_code+';'+this.houseId+'%;';
    }else{
      this.options.where_sql="sp_store.store_num > 0 and sp_store.house_id like ? ";
      this.options.where_type="string";
      this.options.where_value=this.houseId+'%';
    }
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })
  }
  /**
   * 参数
   * @param store 选择过后的保存在这里
   * @param index 
   */
  select(item){ 
    this.selItem = item;
    this.dismiss();
  }

  /**
   * 搜索框
   * 搜索库存存货
   */
  getItems(e){
    clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.options.where_sql="sp_store.store_num > 0 and sp_store.sp_name like ?";
      this.options.where_type="string";
      this.options.where_value='%'+this.searchValue+'%';
      this.doSearch();
    }, 1000);
  }

  /**
   * 执行搜索
   */
  doSearch(){
    this.notification.showLoading();
    this.options.start=0;
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading();
    })
  }

  /**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    this.options.start=0;
    
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root']; 
      }else{
        this.notification.showAlert(res['message']); 
      } 
      refresher.complete();
    })
    
  }

  /**
   * 上拉加载
   * @param infiniteScroll 
   */
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');
    this.options.start += PAGE_COUNT;
    this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        let newItems = res['data']['root'];
        newItems.forEach(item => {
          this.items.push(item);
        });
      }else{
        this.notification.showAlert(res['message']); 
      }
    infiniteScroll.complete();
    });
  } 

  /**
   * 打开高级搜索modal
   * 主要是用在库存的详细查询
   */
  presentModal(){
    let modal = this.modalCtrl.create('StoreFilterPage');
    modal.onDidDismiss(data => {
      this.options.where_sql = data.where_sql||'';
      this.options.where_type = data.where_type||'';
      this.options.where_value = data.where_value||'';
      this.doSearch();
    }) 
    modal.present();
  }
/**
 * 弹出窗口选择出库数量
 * @param event 
 */
  showSelModal(event){
    //alert(event.target);
    const popover = this.popoverCtrl.create('StoreEditPage');
    popover.present({
      ev: event
    });
  }
//关闭
  dismiss() {
    this.items.push('sp_store__out_num','0.0');
    this.items.push('sp_store__out_memo','0.0');
    this.items.push('sp_store__out_price','0.0');
    this.items.push('sp_store__out_money','0.0');
    this.viewCtrl.dismiss(this.selItem);
  }

}
